var eventproxy=require('eventproxy');
var ep=new eventproxy();
function mockReadFile(filename,cb){
    setTimeout(function () {
        if(Math.random()<0.3)return cb({msg:'读取文件错误'});
        cb(null,filename.substring(0,filename.indexOf('.')));
    },Math.random()*1000|0);
}


//--------------------------------

var files=['file_1.txt','file_2.txt','file_3.txt'];

(function(files,cb){

    ep.after('got_file',files.length,function (list) {
        console.log('list:',list);
    });

    ep.on('error', function (err) {
        ep.unbind();
        cb(err);
    });

    for(var i=0;i<files.length;i++){
        mockReadFile(files[i],ep.group('got_file', function (data) {
            return data.split('');
        }));
    }

})(files, function (err, data) {
    if(err)return console.error('error:',err);
    console.log('data:',data);
});


